//
//  AudioCell.m
//  TestAudio
//
//  Created by 悠哉小哥哥 on 2018/7/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "AudioCell.h"


@implementation AudioCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self UI];
    }
    
    return self;
}

- (void)UI{
    
    
    UIView *bgView = [[UIView alloc] init];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.contentView);
    }];
    
    self.clickBtn = [[UIButton alloc] init];
    self.clickBtn.backgroundColor = [UIColor blueColor];
    self.clickBtn.layer.cornerRadius = 20;
    [self.clickBtn setTitle:@"开" forState:UIControlStateNormal];
    [self.clickBtn setTitle:@"关" forState:UIControlStateSelected];
    [self.clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.clickBtn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    [self.clickBtn addTarget:self action:@selector(clickBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.clickBtn];
    
    [self.clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(bgView.mas_left).offset(20);
        make.centerY.equalTo(bgView.mas_centerY);
        make.top.mas_equalTo(bgView.mas_top).offset(20);
        make.bottom.mas_equalTo(bgView.mas_bottom).offset(-20);
        make.width.height.offset(40);
    }];
    
    
    
    self.slider = [[UISlider alloc] init];
    self.slider.minimumValue = 0;
    self.slider.continuous = YES;
    self.slider.minimumTrackTintColor = [UIColor blueColor];
    self.slider.maximumTrackTintColor = [UIColor grayColor];
    [self.slider addTarget:self action:@selector(sliderEvent) forControlEvents:UIControlEventValueChanged];
    
    [bgView addSubview:self.slider];
    
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(bgView);
        make.right.mas_equalTo(bgView.mas_right).offset(-30);
        make.left.mas_equalTo(self.clickBtn.mas_right).offset(30);
    }];
    
    
    self.startLabel = [[UILabel alloc] init];
    self.startLabel.font = [UIFont systemFontOfSize:10];
    self.startLabel.text = @"00:00";
    self.startLabel.textColor = [UIColor orangeColor];
    [bgView addSubview:self.startLabel];
    
    [self.startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.slider);
        make.bottom.mas_equalTo(bgView.mas_bottom).offset(0);
        make.width.offset(50);
    }];
    
    self.finishLabel = [[UILabel alloc] init];
    self.finishLabel.font = [UIFont systemFontOfSize:10];
    self.finishLabel.text = @"--:--";
    self.finishLabel.textColor = [UIColor orangeColor];
    [bgView addSubview:self.finishLabel];
    
    [self.finishLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.slider);
        make.bottom.mas_equalTo(bgView.mas_bottom).offset(0);
        make.width.offset(50);
    }];
    
    
    self.audioPlayer = [AudioSingleton shareAudioPlayer];
    
    
    
}



- (void)setStringAudio:(NSString *)stringAudio{
    
    _stringAudio = stringAudio;

    
    
}



//
- (void)clickBtnClick:(UIButton *)sender{
    
    
    NSIndexPath *indexPath;
    if ([self.audioDelegate respondsToSelector:@selector(audioCellClickControlCell:)] && _audioDelegate != nil)
    {
        indexPath = [self.audioDelegate audioCellClickControlCell:self];
    }
    
   
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        
        self.indexBlock(indexPath);
        
        __weak typeof(self) weakSelf = self;
        self.audioPlayer.playerInfoBlock = ^(CGFloat value, CGFloat totalTime, NSString *timeStr, NSString *totalStr) {
            
            weakSelf.startLabel.text = timeStr;
            weakSelf.finishLabel.text = totalStr;
            weakSelf.slider.value = value;
            
            if (value > 0.999) {
                // 开关变回去
                weakSelf.boolChecked = NO;
            }
        };
        
        [self.audioPlayer playWithName:_stringAudio];
        
        
    }else{
        
        self.boolChecked = NO;
        [self.audioPlayer pause];
    }
    
    
    
    
}



- (void)setBoolChecked:(BOOL)boolChecked{
    
    if (boolChecked) {
        
        self.clickBtn.selected = YES;
        
    }else{
        
        self.clickBtn.selected = NO;
        
    }
    
}


//
- (void)sliderEvent{
    
    [self.audioPlayer playWithValue:self.slider.value];
}

@end
